# -*- coding: utf-8 -*-
import pymysql
from scrapy import cmdline
import base64
import os
import redis


RHOST = '10.200.20.200'
RPORT = 6379
RPASSWORD = 'zhangym930920'
RDB = 1
r = redis.Redis(host=RHOST, port=RPORT, db=RDB,charset='utf-8',password=RPASSWORD,decode_responses=True)


while True:
    datas = r.brpop('hotel_users_qunar')[1]
    if datas:
        z = eval(datas)
        CRS_ID = z[2]
        username = z[0]
        password = base64.b64encode(str(z[1]).encode())
        batch_no = z[3]

        begin='scrapy crawl ota_qunar_hotel -a CRS_ID=%s -a username=%s -a password=%s -a batch_no=%s'%(CRS_ID,username,password,batch_no)
        os.system(begin)
    else:
        break



# HOST='127.0.0.1'
# PORT=3306
# USER='root'
# PASSWD=''
# DB='ceshi'
# CHARSET='utf8mb4'
#
# conn = pymysql.connect(host=HOST, port=PORT, user=USER, passwd=PASSWD, db=DB,charset=CHARSET)
# cur=conn.cursor()
# sql="select Elong_Username,Elong_Password from hotel_users"
# cur.execute(sql)
# users = cur.fetchall()
#
#
#
# z = []
# for user in users:
#     if user[0]!=None and user[1]!=None:
#         z.append(user[0])
# #         if user[0] not in z:
# #             z.append(user[0])
# #         else:
# #             print(user[0])
# #
# #
# print(z)
#     # pa = ''
    # for i in password:
    #     if i == '0':
    #         pa += '9'
    #     elif i == 'A':
    #         pa += 'Z'
    #     elif i == 'a':
    #         pa += 'z'
    #     elif i.isdigit():
    #         pa += chr(ord(i) - 1)
    #     elif i.isalpha():
    #         pa += chr(ord(i) - 1)
    #     else:
    #         pa += i

    # cmdline.execute(('scrapy crawl ota_qunar_hotel -a username=%s -a password=%s'%(username,pa)).split())
    # cmdline.execute(('scrapy crawl ota_qunar_hotel -a username=%s -a password=%s'%(username,password)).split())

    # begin = 'scrapy crawl qunar -a username=%s -a password=%s'%(username,password)
    # os.system(begin)